package cn.com.header.web.service.impl.sys;

import cn.com.header.web.mapper.sys.RoleAuthorityDAO;
import cn.com.header.web.model.sys.RoleAuthority;
import cn.com.header.web.service.sys.RoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yds on 2017/08/11
 */
@Service
public class RoleAuthorityServiceImpl implements RoleAuthorityService{

    @Autowired
    private RoleAuthorityDAO roleAuthorityDAO;

    @Override
    public List<RoleAuthority> findByRoleId(Long roleId) {
        return roleAuthorityDAO.findByRoleId(roleId);
    }

    @Override
    public void save(RoleAuthority ra,String authorityIds) {
        List<RoleAuthority> roleAuthorityList = roleAuthorityDAO.findByRoleId(ra.getRoleId());
        if(roleAuthorityList.size() > 0){
            for (RoleAuthority rh : roleAuthorityList) {
                roleAuthorityDAO.delete(rh.getId());
            }

        }
        String[] authorityId = authorityIds.split(",");
        for (int i = 0; i < authorityId.length; i++) {
            ra.setAuthorityId(Long.parseLong(authorityId[i]));
            ra.setRoleId(ra.getRoleId());
            roleAuthorityDAO.insert(ra);
        }
    }
}
